package com.fjb.service.pay.impl;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.utils.NetworkUtil;
import com.fjb.dto.LoginUserInfo;
import com.fjb.service.pay.WxPayMyService;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;

@Service
public class WxPayMyServiceImpl implements WxPayMyService{
	
	private static final Logger logger = LoggerFactory.getLogger(WxPayMyServiceImpl.class);
	
	@Autowired
    private WxPayService wxPayService;
	
	@Override
	public JsonResult<WxPayMpOrderResult> addOrderPay(HttpServletRequest request, Integer orderId,LoginUserInfo userInfo) {
		Integer userId = userInfo.getUserId();
		String outTradeNo = "";
		String openid = "";
		
		WxPayMpOrderResult result = null;
        WxPayUnifiedOrderRequest wxPayorderRes = new WxPayUnifiedOrderRequest();
        wxPayorderRes.setOutTradeNo(outTradeNo);
        wxPayorderRes.setOpenid(openid);
        wxPayorderRes.setBody("订单： Body");
        // 订单金额
        BigDecimal orderPrice = new BigDecimal(0);
        // 元转成分
        int totalFee = 0;
        totalFee = orderPrice.multiply(new BigDecimal(100)).intValue();
        wxPayorderRes.setTotalFee(totalFee);
        String ipAddress = NetworkUtil.getIpAddress(request);
        wxPayorderRes.setSpbillCreateIp(ipAddress);
        try {
			result = wxPayService.createOrder(wxPayorderRes);
		} catch (WxPayException e) {
			e.printStackTrace();
		}
        //缓存prepayID用于后续模版通知	
        String prepayId = result.getPackageValue();
        prepayId = prepayId.replace("prepay_id=", "");
		
		return new JsonResult<WxPayMpOrderResult>(result, HttpCode.PARAM_VERIFICATION);
	}

	@Override
	public String updateOrderNotify(String xmlResult) {
		WxPayOrderNotifyResult result = null;
        try {
            result = wxPayService.parseOrderNotifyResult(xmlResult);
            if(!WxPayConstants.ResultCode.SUCCESS.equals(result.getResultCode())){
                logger.error(xmlResult);
                throw new WxPayException("微信通知支付失败！");
            }
            if(!WxPayConstants.ResultCode.SUCCESS.equals(result.getReturnCode())){
                logger.error(xmlResult);
                throw new WxPayException("微信通知支付失败！");
            }
        } catch (WxPayException e) {
            e.printStackTrace();
            return WxPayNotifyResponse.fail(e.getMessage());
        }

        logger.info("处理腾讯支付平台的订单支付");
        logger.info(result.toString());

        String outTradeNo = result.getOutTradeNo();
        String payId = result.getTransactionId();
        // 分转化成元
        String totalFee = BaseWxPayResult.fenToYuan(result.getTotalFee());
        
        return WxPayNotifyResponse.success("处理成功!");
	}
	
	
	
}
